package com.hz.monitoring.module.hardware.service.sensor_sys_type;

import cn.hutool.core.collection.CollectionUtil;
import com.hz.monitoring.framework.common.pojo.PageResult;
import com.hz.monitoring.module.hardware.controller.admin.sensor_sys_type.vo.*;
import com.hz.monitoring.module.hardware.convert.sensor_sys_type.SensorSysTypeConvert;
import com.hz.monitoring.module.hardware.dal.dataobject.sensor_sys_type.SensorSysTypeDO;
import com.hz.monitoring.module.hardware.dal.mysql.sensor_sys_type.SensorSysTypeMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static com.hz.monitoring.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.hz.monitoring.module.hardware.enums.ErrorCodeConstants.SENSOR_SYS_TYPE_EXISTS;
import static com.hz.monitoring.module.hardware.enums.ErrorCodeConstants.SENSOR_SYS_TYPE_NOT_EXISTS;

/**
 * 传感器系统分类 Service 实现类
 *
 * @author lin
 */
@Service
@Validated
public class SensorSysTypeServiceImpl implements SensorSysTypeService {

    @Resource
    private SensorSysTypeMapper sensorSysTypeMapper;

    @Override
    public Long createSensorSysType(SensorSysTypeCreateReqVO createReqVO) {
        // 插入
        SensorSysTypeDO sensorSysType = SensorSysTypeConvert.convert(createReqVO);
        List<SensorSysTypeDO> list = getSensorSysTypeList(SensorSysTypeListReqVO.builder().model(createReqVO.getModel()).build());
        if (CollectionUtil.isNotEmpty(list))
            throw exception(SENSOR_SYS_TYPE_EXISTS);

        sensorSysTypeMapper.insert(sensorSysType);
        // 返回
        return sensorSysType.getId();
    }

    @Override
    public void updateSensorSysType(SensorSysTypeUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateSensorSysTypeExists(updateReqVO.getId());
        // 更新
        SensorSysTypeDO updateObj = SensorSysTypeConvert.convert(updateReqVO);
        sensorSysTypeMapper.updateById(updateObj);
    }

    @Override
    public void deleteSensorSysType(Long id) {
        // 校验存在
        this.validateSensorSysTypeExists(id);
        // 删除
        sensorSysTypeMapper.deleteById(id);
    }

    private void validateSensorSysTypeExists(Long id) {
        if (sensorSysTypeMapper.selectById(id) == null) {
            throw exception(SENSOR_SYS_TYPE_NOT_EXISTS);
        }
    }

    @Override
    public SensorSysTypeDO getSensorSysType(Long id) {
        return sensorSysTypeMapper.selectById(id);
    }

    @Override
    public List<SensorSysTypeDO> getSensorSysTypeList(SensorSysTypeListReqVO cmd) {
        return sensorSysTypeMapper.selectList(cmd);
    }

    @Override
    public PageResult<SensorSysTypeDO> getSensorSysTypePage(SensorSysTypePageReqVO pageReqVO) {
        return sensorSysTypeMapper.selectPage(pageReqVO);
    }

    @Override
    public List<SensorSysTypeDO> getSensorSysTypeList(SensorSysTypeExportReqVO exportReqVO) {
        return sensorSysTypeMapper.selectList(exportReqVO);
    }

}
